package com.tsmti.lab.controller.activiti;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.tsmti.activiti.entity.ExtendActBusiness;
import com.tsmti.activiti.service.ExtendActBusinessService;
import com.tsmti.core.common.controller.BaseController;
import com.tsmti.core.common.entity.TreeNode;
import com.tsmti.core.common.entity.TreeNodeHelper;
import com.tsmti.core.common.model.AjaxJson;
import com.tsmti.core.common.query.data.PropertyPreFilterable;
import com.tsmti.core.common.query.data.Queryable;
import com.tsmti.core.util.JsonUtils;
import com.tsmti.core.util.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.criterion.DetachedCriteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Package：com.tsmti.lab.controller.activiti
 * @Class：ExtendActBusinessController
 * @Description： TODO
 * @Author：zcw
 * @Date：Created in 2018/8/17 10:12
 * @Company:
 * @Version：
 * @Modified By:
 *
 */
@RequestMapping("act/bus")
@Controller
public class ExtendActBusinessController extends BaseController{
    protected Logger logger = LoggerFactory.getLogger(ExtendActBusinessController.class);

    @Autowired
    ExtendActBusinessService businessService;

    @RequestMapping("busTree")
    @RequiresPermissions("act:bus:all")
    public String busTree(Model model , ExtendActBusiness extendActBusiness){
        List<ExtendActBusiness> businessEntityList = businessService.queryListByBean(extendActBusiness);
        model.addAttribute("busTree", JsonUtils.getJsonByObj(businessEntityList));
        model.addAttribute("data",extendActBusiness);
        return "activiti/busTree";
    }

    /**
     * 保存或者更新
     * @param model
     * @param businessEntity
     * @return
     */
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("act:bus:all")
    public AjaxJson edit(Model model , ExtendActBusiness businessEntity){
        AjaxJson ajaxJson = AjaxJson.success();
        try {
            businessService.edit(businessEntity);
            ExtendActBusiness bus = businessService.get(businessEntity.getId());
            ajaxJson = AjaxJson.success("保存名称【"+businessEntity.getName()+"】成功",bus);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("保存失败",e);
            ajaxJson = AjaxJson.fail("保存失败："+e.getMessage());
        }
        return ajaxJson;
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @RequestMapping(value = "info",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("act:bus:all")
    public AjaxJson info(String id, HttpServletRequest request){
        ExtendActBusiness businessEntity = businessService.get(id);
        return AjaxJson.success("ok",businessEntity);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "del",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("act:bus:all")
    public AjaxJson del(String id, HttpServletRequest request){
        businessService.removeById(id);
        return AjaxJson.success("删除成功");
    }

}
